﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace ru.kefir.lab_royak_wf.ru.viventus.lab_royak_wf.fileManager
{
    class fileManager
    {
        private List<Graph> collGraph; // коллекция для рисовалки, Graph - это это элемент коллекции, то есть график

        public void setManager(Graph Gr)
        {
            this.collGraph.Add(Gr);
        }

        public void setManager(List<Graph> GR)
        {
            this.collGraph = GR;
        }

        public List<Graph> getManager()
        {
            return this.collGraph;
        }

        public fileManager()
        {
            this.collGraph = new List<Graph>();
        }

        public void deleteAllGraphics() // очистка от всех графиков
        {
            this.collGraph.Clear();
        }

        public void openFile()
        {
            Graph f1 = new Graph();
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (String file in openFileDialog1.FileNames) // открывает файлы по очереди сверху вниз
                {
                    openFileDialog1.FileName = file;
                    if (new FileInfo(file).Length ==0)
                    {
                        MessageBox.Show("File is empty");
                    }
                    else
                        try
                        {
                            if ((myStream = openFileDialog1.OpenFile()) != null)
                            {
                                using (myStream)
                                {
                                    string line;
                                    StreamReader strread = new StreamReader(myStream);
                                    while ((line = strread.ReadLine()) != null)
                                    {
                                        string[] items = new string[2];
                                        items = line.Split(' ');        // разделяет x от y
                                        double x, y;
                                        x = double.Parse(items[0]);
                                        y = double.Parse(items[1]);
                                        f1.setCoord(x, y);
                                    }
                                    collGraph.Add(f1);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                        }
                }
            }
        }
    }
}
